package com.bjnangle.nangletool;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by YFB on 2017/8/21.
 */

public class NetActivity extends BaseActivity {
    private StringBuffer stringBuffer = new StringBuffer();
    private TextView textView ;
    private EditText edIp;
    private Context context = NetActivity.this;

    private TextView tvTitle;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net);
        textView = (TextView)findViewById(R.id.tv_net_log);
        setActivity(this);
        edIp = (EditText)findViewById(R.id.edit_ip);
        ((Button)findViewById(R.id.btn_ping)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ping();
            }
        });
        edIp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int length = charSequence.length();
                if(length>0){
                    CharSequence c = charSequence.subSequence(length-1,length);
                    Log.e("dddd","------------->"+c);
                    if(c.toString().equals("*")||charSequence.toString().contains("*")){
                        NetActivity.this.finish();
                    }else if(c.toString().equals("#")){
                        CharSequence c1 = charSequence.subSequence(0,length-1);
                        edIp.setText(c1+".");
                        Editable etext = edIp.getText();
                        Selection.setSelection(etext, etext.length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        ivGoBack = (ImageView)findViewById(R.id.btn_back);
        tvTitle = (TextView)findViewById(R.id.tv_title);
        tvTitle.setText("网络检测");

    }
    private void ping(){
        String ip = edIp.getText().toString();
        if(ip.equals("")){
            Toast.makeText(NetActivity.this,"ip不能为空",Toast.LENGTH_LONG).show();
        }else{
            runPingIPprocess(ip);
        }
    }
    public void runPingIPprocess(final String ipString)
    {
        final Thread pingThread = new Thread()
        {
            @Override
            public void run()
            {
                try{
                    String line = null;
                    String command = "ping -c " + 3 + " -w 5 " + ipString;
                    Process process =  Runtime.getRuntime().exec(command);
                    if (process != null) {
                        BufferedReader successReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                        while ((line = successReader.readLine()) != null) {
                            Log.e("NetActivity",line+"----->");
                            //append(stringBuffer, line);
                            Message message = new Message();
                            message.what = 0;
                            message.obj = line;
                            handler.sendMessage(message);
                        }
                        int status = process.waitFor();
                        if (status == 0) {
                            Log.e("NetActivity","exec cmd success:" + command);
                            Message message = new Message();
                            message.what = 0;
                            message.obj = "exec cmd success:";
                            handler.sendMessage(message);
                        } else {
                            Log.e("NetActivity", "exec cmd fail.");
                            Message message = new Message();
                            message.what = 0;
                            message.obj = "exec cmd fail.";
                            handler.sendMessage(message);
                        }
                        Log.e("NetActivity",  "exec finished.");
                        Message message = new Message();
                        message.what = 0;
                        message.obj = "exec finished.";
                        handler.sendMessage(message);
                    }

                } catch (Exception e){
                    Log.e("Light", e.getLocalizedMessage());
                } finally{

                }
            }
        };
        pingThread.start();
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                String text = (String)msg.obj;
                stringBuffer.append(text + "\n");
                textView.setText(stringBuffer.toString());
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_PROG_BLUE:
                ping();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
